
+function(mui, app) {
	// Cookie说明:
	// AUTH_ACCOUNT=登录账号, 在登录成功/注册成功/找回密码成功后记录
	// TEMP_ACCOUNT=临时账号, 只保留10分钟, 用于在登录页面输入账号后向注册/找回密码页面传递参数
	app.getCookiePath = function() {
		var base = $(document.head).find("meta[name=relative-path]").attr("content");
		return base + "auth/";
	};
	app.getCookieExpires = function(seconds) {
		var expires = new Date();
		expires.setMilliseconds(expires.getMilliseconds() + seconds * 1000);
		return expires;
	};
	app.toMainPage = function(json) { // this = form
		// 登录成功, 跳转至首页, backurl="/"有时候会跳到不存在的根路径, 改成跳到项目首页
		var backurl = json.body && json.body.backurl;
		if (!backurl || backurl == "/") { backurl = $(this).attr("data-to"); }
		window.location.replace(backurl);
	};

	// 发送短信验证码
	app.doSendSmsCode = function(e) {
		e.preventDefault();
		var btn = $(this);
		btn.attr("disabled", "disabled");
		var form = btn.closest("form");
		form.find("input[name=account]").zhhvalidate({ tips:false }, function(e) {
	        if (e.passed) { // 校验通过, 提交
				form.find(".prompt-message").text("");
				var url = btn.attr("data-url");
				// data-fields="['formDataField1','formDataField2']"
				// data-fields="['formDataField1',{paramField2:'formDataField2',paramField3:'formDataField3'}]"
				var fields = $.zhh.parseOptions(btn.attr("data-fields") || "account");
				if (!$.isArray(fields)) { fields = [fields]; }
				var data = {};
				$.each(fields, function(i, field) {
					if ($.isPlainObject(field)) {
						for (var key in field) {
							data[key] = form.find("[name='"+field[key]+"']").val();
						}
					} else {
						data[field] = form.find("[name='"+field+"']").val();
					}
				});
				// 调用接口发送短信验证码
				$.zajax(url, data, {
					succ:function(json) {
						Utils.succTips("短信验证码发送成功");
						form.find("input[name=validCode]").focus().select();
						btn.countdown(60, function() {
							btn.removeAttr("disabled");
						});
					},
					failTips:function(json) {
						btn.removeAttr("disabled");
						form.find(".prompt-message").text(json && json.message || "网络异常, 请稍候重试");
					}
				});
	        } else { // 校验不通过, 显示错误提示
	        	$(this).focus().select();
				btn.removeAttr("disabled");
				form.find(".prompt-message").text(e.text);
	        }
	    });
	};
	app.showRequiredPrompt = function(elem, focus) {
		var form = elem.closest("form");
		var text = elem.attr("data-label");
		if (!text) {
			var label = elem.prevAll("label");
			if (label.length) { text = label.text().replace(/[:：]$/g, ""); }
		}
		if (!text) {
			text = elem.attr("placeholder");
		}
		form.find(".prompt-message").text("请输入" + (text || "必填项"));
		if (focus) { elem.focus(); }
	};

	/** 获取本地是否安装客户端 **/
	app.isInstalled = function(id) {
		if (id === 'qihoo' && mui.os.plus) {
			return true;
		}
		if (mui.os.android) {
			var main = plus.android.runtimeMainActivity();
			var packageManager = main.getPackageManager();
			var PackageManager = plus.android.importClass(packageManager)
			var packageName = {
				"qq": "com.tencent.mobileqq",
				"weixin": "com.tencent.mm",
				"sinaweibo": "com.sina.weibo"
			}
			try {
				return packageManager.getPackageInfo(packageName[id], PackageManager.GET_ACTIVITIES);
			} catch (e) {}
		} else {
			switch (id) {
				case "qq":
					var TencentOAuth = plus.ios.import("TencentOAuth");
					return TencentOAuth.iphoneQQInstalled();
				case "weixin":
					var WXApi = plus.ios.import("WXApi");
					return WXApi.isWXAppInstalled()
				case "sinaweibo":
					var SinaAPI = plus.ios.import("WeiboSDK");
					return SinaAPI.isWeiboAppInstalled()
				default:
					break;
			}
		}
	};
}(mui, window.app = {});

